
/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */

package com.jav.per;

import com.jav.Datos;
import com.jav.inter.Constantes;
import com.jav.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;


public class PersisPedido implements Constantes{

    private int numero;
    private String cliente;
    private String dt_inicio;
    private long venta;
    private long cambio;
    private final String ATENDIDO = "1";
    private final String RAZONNO = "1";


    public String toString(){
        StringBuffer st = new StringBuffer();
        /*st.append(COMI+StringUtils.lpad(Integer.toString(getNumero()),'0',7)+COMI+SEPA+
                COMI+StringUtils.lpad(getCliente(),'0',7)+COMI+SEPA+
                getDt_inicio()+SEPA+
                COMI+StringUtils.lpad(Long.toString(getVenta()),'0',10)+COMI+SEPA+
                COMI+StringUtils.lpad(Long.toString(getCambio()),'0',10)+COMI);
        */
        st.append(COMI+Integer.toString(getNumero())+COMI+SEPA+
                COMI+getCliente()+COMI+SEPA+
                COMI+getDt_inicio()+COMI+SEPA+
                COMI+ATENDIDO+COMI+SEPA+
                COMI+RAZONNO+COMI+SEPA+
                COMI+Long.toString(getVenta())+COMI+SEPA+
                COMI+Long.toString(getCambio())+COMI);

        return st.toString();
    }
    
    public String toStringWeb(){
        StringBuffer st = new StringBuffer();
        st.append(Integer.toString(getNumero())).append(SEPA2).
                append(getCliente()).append(SEPA2+";");

        return st.toString();
    }

    public String getDt_inicio() {
        return dt_inicio;
    }

    public void setDt_inicio() {
        Calendar cal = Calendar.getInstance();
        Date d = new Date();
        cal.setTime(d);
        this.dt_inicio = cal.get(Calendar.YEAR)+"-"+format(cal.get(Calendar.MONTH))+"-"+ format(cal.get(Calendar.DAY_OF_MONTH));
        this.dt_inicio += " "+format(cal.get(Calendar.HOUR_OF_DAY))+":"+format(cal.get(Calendar.MINUTE))+":"+format(cal.get(Calendar.SECOND));
    }



    public long getCambio() {
        return cambio;
    }

    public void setCambio(long cambio) {
        this.cambio = cambio;
    }

    public String getCliente() {
        return cliente;
    }

    public void setCliente(String cliente) {
        this.cliente = cliente;
    }

    public int getNumero() {
        return numero;
    }

    public int getConsec(){
        return Datos.getInstance().getMaxPedido();
    }
    
    public void setNumero(int numero) {
        this.numero = numero;
    }



    public long getVenta() {
        return venta;
    }

    public void setVenta(long venta) {
        this.venta = venta;
    }

    public String format (int dat){
        String dato = String.valueOf(dat);
        String resp = "";
        if (dato == null){
            return "00";
        }
        else{
            resp = dato.trim();
            if (resp.length() > 1){
                return resp;
            }
            else{
                resp = "0"+resp;
            }
        }

        return resp;

    }



    public byte[] persist() throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        try{
            dout.writeInt(getNumero());
            dout.writeUTF(getCliente());
            dout.writeUTF(getDt_inicio());
            dout.writeLong(getVenta());
            dout.writeLong(getCambio());
            dout.flush();
        }
        finally{
            if(dout!= null){
                 dout.close();
            }
            if(bout!= null){
                 bout.close();
            }
        }
        return bout.toByteArray();

    }

    public void resurrect(byte[] by) throws IOException {
        ByteArrayInputStream bin = new ByteArrayInputStream(by);
        DataInputStream din = new DataInputStream(bin);
        setNumero(din.readInt());
        setCliente(din.readUTF());
        dt_inicio = din.readUTF();
        setVenta(din.readLong());
        setCambio(din.readLong());
        bin.close();
        din.close();
    }


}
